package org.orz.story.works.biz.clubhouse.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.orz.cloud.auth.UserUtil;
import org.orz.cloud.auth.domain.UserInfo;
import org.orz.cloud.common.enums.AliveFlagEnum;
import org.orz.story.works.biz.clubhouse.model.req.AddDiaryReq;
import org.orz.story.works.biz.clubhouse.model.req.DeleteDiaryReq;
import org.orz.story.works.biz.clubhouse.model.req.UpdateDiaryReq;
import org.orz.story.works.biz.clubhouse.model.resp.ListWorksDiaryResp;
import org.orz.story.works.biz.clubhouse.persistence.entities.WorksDiary;
import org.orz.story.works.biz.clubhouse.persistence.mappers.WorksDiaryMapper;
import org.orz.story.works.biz.clubhouse.service.WorksDiaryService;
import org.orz.story.works.common.exception.WorksException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static org.orz.story.works.common.exception.WorksException.Type.WORKS_0001;

@Service
public class WorksDiaryServiceImpl implements WorksDiaryService {
    @Autowired
    private WorksDiaryMapper worksDiaryMapper;


    @Override
    public void addDiary(AddDiaryReq req) {
        UserInfo userInfo = UserUtil.getUserInfoWithExistCheck();
        WorksDiary diary = new WorksDiary();
        diary.setAccountId(userInfo.getAccountId());
        diary.setContent(req.getContent());
        diary.setCreateUser(userInfo.getAccountId());
        diary.setCreateDate(new Date());
        diary.setUpdateUser(userInfo.getAccountId());
        diary.setUpdateDate(new Date());
        worksDiaryMapper.insert(diary);
    }

    @Override
    public void updateDiary(UpdateDiaryReq req) {
        UserInfo userInfo = UserUtil.getUserInfoWithExistCheck();

        WorksDiary diary = worksDiaryMapper.selectById(req.getId());
        if(!diary.getAccountId().equals(userInfo.getAccountId())){
            throw new WorksException(WORKS_0001);
        }

        WorksDiary updateParam = new WorksDiary();
        updateParam.setId(req.getId());
        updateParam.setContent(req.getContent());
        updateParam.setUpdateUser(userInfo.getAccountId());
        updateParam.setUpdateDate(new Date());
        worksDiaryMapper.updateById(updateParam);
    }

    @Override
    public void deleteDiary(DeleteDiaryReq req) {
        UserInfo userInfo = UserUtil.getUserInfoWithExistCheck();

        WorksDiary diary = worksDiaryMapper.selectById(req.getId());
        if(!diary.getAccountId().equals(userInfo.getAccountId())){
            throw new WorksException(WORKS_0001);
        }

        WorksDiary updateParam = new WorksDiary();
        updateParam.setId(req.getId());
        updateParam.setAliveFlag(AliveFlagEnum.DELETED.getKey());
        updateParam.setUpdateUser(userInfo.getAccountId());
        updateParam.setUpdateDate(new Date());
        worksDiaryMapper.updateById(updateParam);
    }

    @Override
    public List<ListWorksDiaryResp> listWorksDiary() {
        UserInfo userInfo = UserUtil.getUserInfoWithExistCheck();
        LambdaQueryWrapper<WorksDiary> wrapper = new LambdaQueryWrapper<WorksDiary>()
                .eq(WorksDiary::getAccountId, userInfo.getAccountId())
                .eq(WorksDiary::getAliveFlag, AliveFlagEnum.ALIVE.getKey());
        wrapper.orderByDesc(WorksDiary::getId);
        List<WorksDiary> worksDiaries = worksDiaryMapper.selectList(wrapper);
        if(CollUtil.isEmpty(worksDiaries)){
            return Collections.emptyList();
        }

        return worksDiaries.stream().map(item -> {
            ListWorksDiaryResp resultData = new ListWorksDiaryResp();
            resultData.setId(item.getId());
            resultData.setCreateDate(item.getCreateDate());
            resultData.setUpdateDate(item.getUpdateDate());
            resultData.setContent(item.getContent());
            return resultData;
        }).collect(Collectors.toList());
    }
}
